package com.mjk.manager.gen.impl.ui;

import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.core.jdbc.annotation.ExampleField;
import com.mjk.common.core.jdbc.annotation.Id;
import com.mjk.common.core.jdbc.util.provider.SqlProvider;
import com.mjk.common.util.FileUtils;
import com.mjk.common.util.StrUtils;
import com.mjk.manager.gen.dto.GenDto;

import java.lang.reflect.Field;
import java.util.List;
/**
 * 接口增删该查生成
 * Dto类更具examplefile生成
 * @author Administrator
 *
 */
public class WorkGenerate implements IUiGen{
	
	private static final String WORKNAME = "WorkProxy";
	/**
	 * 包名需要去除的部分
	 */
	private static final String PACKAGE_SMPARK = "src\\main\\java\\";

	@Override
	public Class<?> createClass(GenDto model) {
		String tableName = getTableName(model);
		String dir = model.getServiceProjectPath()+model.getServicePath()+tableName.toLowerCase()+"\\";
		String listFileName = StrUtils.toUpperCaseFirstOne(tableName) + WORKNAME+".java";
		String listM = getWorkGenerate(model);
		FileUtils.write2File(dir, listFileName, listM);
		String dtoDir = dir+"dto\\";
		String dto = getDtoGenerate(model);
		String dtoFileName = StrUtils.toUpperCaseFirstOne(tableName) + "Dto.java";
		FileUtils.write2File(dtoDir, dtoFileName, dto);
		return null;
	}
	/**
	 * 生成controller
     */
	private String getWorkGenerate(GenDto dto) {
		BaseEntity entity = dto.getEntity();
		StringBuilder sb = new StringBuilder();
		String tableName = getTableName(entity);
		String dtoName = StrUtils.toUpperCaseFirstOne(tableName)+"Dto";
		sb.append("package "+getPackage(dto)).append(tableName.toLowerCase()).append(";").append(WRAP);
		sb.append("import "+getPackage(dto)).append(tableName.toLowerCase()).append(".dto.").append(dtoName).append(";").append(WRAP);
		sb.append("import com.mjk.common.base.response.BeeResponse;"+WRAP);
		sb.append("import com.mjk.common.base.work.BaseWork;"+WRAP);
		sb.append("import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;"+WRAP);
		sb.append("import com.mjk.common.base.work.annotation.BeeWork;"+WRAP);
		sb.append("import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;"+WRAP);
		sb.append("import com.mjk.common.util.ObjectUtils;"+WRAP);
		sb.append("import com.mjk.common.base.error.BeeError;"+WRAP);
		sb.append("import org.springframework.beans.factory.annotation.Autowired;"+WRAP);
		sb.append(WRAP);
		sb.append("@BeeWork(work = \"").append(tableName.toLowerCase()).append("\")").append(WRAP);
		sb.append("public class ").append(StrUtils.toUpperCaseFirstOne(tableName)).append(WORKNAME).append(" extends BaseWork{").append(WRAP);
		sb.append(TAB+"@Autowired"+WRAP);
		sb.append(TAB+"private BeeDBService baseDBService;"+WRAP);
		sb.append(WRAP);
		sb.append(TAB+"/**"+WRAP);
		sb.append(TAB + " * 获取").append(tableName).append("列表信息").append(WRAP);
		sb.append(TAB+" * @param dto"+WRAP);
		sb.append(TAB+" * @return"+WRAP);
		sb.append(TAB+" */"+WRAP);
		sb.append(TAB + "public  BeeResponse get").append(StrUtils.toUpperCaseFirstOne(tableName)).append("List(").append(dtoName).append(" dto){").append(WRAP);
		sb.append(TAB_2).append("BeeQueryWrapper wrapper = new BeeQueryWrapper(new ").append(entity.getClass().getName()).append("());").append(WRAP);
		//迭代条件属性
        List<Field> fields = SqlProvider.getValidTableFiled(entity);
		String idFiled = "";
		for(Field f : fields) {
			ExampleField example  = f.getAnnotation(ExampleField.class);
			Id id = f.getAnnotation(Id.class);
			if(null!=example) {
				sb.append(TAB_2 + "if(ObjectUtils.isNotEmpty(dto.get").append(StrUtils.toUpperCaseFirstOne(f.getName())).append("())) {").append(WRAP);
				sb.append(TAB_3).append("wrapper.eq(").append(entity.getClass().getName()).append("::get"+StrUtils.toUpperCaseFirstOne(f.getName())).append(",dto.get"+StrUtils.toUpperCaseFirstOne(f.getName())+"());").append(WRAP);
				sb.append(TAB_2+"}"+WRAP);
			}
			if(null!=id) {
				idFiled = f.getName();
			}
		}
		sb.append(TAB_2+"if(isPage(dto)) {"+WRAP);
		sb.append(TAB_3+"wrapper.setPage(dto.getPageStart(),dto.getPageSize());"+WRAP);
		sb.append(TAB_3+"return BeeError.BEE_SUC.tranceError(baseDBService.findManyByWrapperWithPage(wrapper));"+WRAP);
		sb.append(TAB_2+"}else {"+WRAP);
 		sb.append(TAB_3+"return BeeError.BEE_SUC.tranceError(baseDBService.findManyByWrapper(wrapper));"+WRAP);
 		sb.append(TAB_2+"}"+WRAP);
 		sb.append(TAB+"}"+WRAP);
 		sb.append(TAB+"/**"+WRAP);
		sb.append(TAB + " * 保存或修改").append(tableName).append(WRAP);
		sb.append(TAB+" * @param model"+WRAP);
		sb.append(TAB+" * @return"+WRAP);
		sb.append(TAB+" */"+WRAP);
		sb.append(TAB + "public  BeeResponse saveOrUpdate").append(StrUtils.toUpperCaseFirstOne(tableName)).append("(").append(entity.getClass().getName()).append(" model){").append(WRAP);
		sb.append(TAB_2 + "if(ObjectUtils.isNotEmpty(model.get").append(StrUtils.toUpperCaseFirstOne(idFiled)).append("())) {").append(WRAP);
		sb.append(TAB_3).append("BeeQueryWrapper wrapper = new BeeQueryWrapper(model);").append(WRAP);
		sb.append(TAB_3).append("wrapper.eq("+entity.getClass().getName()+"::get"+StrUtils.toUpperCaseFirstOne(idFiled)+",model.get"+StrUtils.toUpperCaseFirstOne(idFiled)+"());").append(WRAP);
		sb.append(TAB_3+"baseDBService.update(wrapper);"+WRAP);
		sb.append(TAB_2+"}else {"+WRAP);
		sb.append(TAB_3+"baseDBService.save(model);"+WRAP);
		sb.append(TAB_2+"}"+WRAP);
		sb.append(TAB_2+"return BeeError.BEE_SUC.tranceError();"+WRAP);
		sb.append(TAB+"}"+WRAP);
		sb.append(TAB+"/**"+WRAP);
		sb.append(TAB + " * 删除").append(tableName).append(WRAP);
		sb.append(TAB+" * @param model"+WRAP);
		sb.append(TAB+" * @return"+WRAP);
		sb.append(TAB+" */"+WRAP);
		sb.append(TAB + "public  BeeResponse del").append(StrUtils.toUpperCaseFirstOne(tableName)).append("(").append(entity.getClass().getName()).append(" model){").append(WRAP);
		sb.append(TAB_2 + "needParams(model, \"").append(idFiled).append("\");").append(WRAP);
		sb.append(TAB_2).append("BeeQueryWrapper wrapper = new BeeQueryWrapper(model);").append(WRAP);
		sb.append(TAB_2).append("wrapper.eq("+entity.getClass().getName()+"::get"+StrUtils.toUpperCaseFirstOne(idFiled)+",model.get"+StrUtils.toUpperCaseFirstOne(idFiled)+"());").append(WRAP);
		sb.append(TAB_2+"baseDBService.delete(wrapper);"+WRAP);
		sb.append(TAB_2+"return BeeError.BEE_SUC.tranceError();"+WRAP);
		sb.append(TAB+"}"+WRAP);
		sb.append("}");
		return sb.toString();
	}
	
	private String getDtoGenerate(GenDto dto) {
		StringBuilder sb = new StringBuilder();
		String tableName = getTableName(dto.getEntity());
		sb.append("package "+getPackage(dto)).append(tableName.toLowerCase()).append(".dto;").append(WRAP);
		sb.append("import com.mjk.common.base.request.BaseDto;"+WRAP);
		sb.append("import lombok.Data;"+WRAP);
		sb.append("@Data"+WRAP);
		sb.append("public class ").append(StrUtils.toUpperCaseFirstOne(tableName)).append("Dto extends BaseDto{").append(WRAP);
        List<Field> fields = SqlProvider.getValidTableFiled(dto.getEntity());
		for(Field f : fields) {
			ExampleField example  = f.getAnnotation(ExampleField.class);
			if(null!=example) {
				sb.append(TAB + "private ").append(f.getGenericType().getTypeName()).append(" ").append(f.getName()).append(";").append(WRAP);
			}
		}
		sb.append("}");
		return sb.toString();
	}

	private String getPackage(GenDto dto){
		return dto.getServicePath().replace(PACKAGE_SMPARK,"").replace("\\",".");
	}

}
